package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.entity.Role;
import com.atguigu.crowd.service.api.RoleService;
import com.atguigu.crowd.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class RoleHandler {
    @Autowired
    private RoleService roleService ;
    @ResponseBody
    @RequestMapping(value = "/role/get/page/info.json")
    public ResultEntity<PageInfo<Role>> getPageInfo(// 指定默认值
                    @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                    @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                    @RequestParam(value = "keyword",defaultValue = "") String keyword){
        PageInfo<Role> pageInfo = roleService.getPageInfo(pageNum, pageSize, keyword);
        //封装到resultentity对象中返回（如果上面的操作抛出异常，交给映射机制处理）
        return ResultEntity.successWithData(pageInfo) ;
    }
    @ResponseBody
    @RequestMapping("role/save.json")
    public ResultEntity<String> saveRole(Role role){
        List<Role> roleByName = roleService.findRoleByName(role);
        if(roleByName !=null){
            return ResultEntity.failed("命名重复，请重新输入！");
        }
        roleService.saveRole(role);
        return ResultEntity.successWithoutData();
    }
    @ResponseBody
    @RequestMapping("role/update.json")
    public ResultEntity<String> editRole(Role role){
        roleService.updateRole(role);
        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("/role/remove/by/role/id/array.json")
    public ResultEntity<String> removeByRoleIdArray(@RequestBody List<Integer> roleIdList){
        roleService.removeRole(roleIdList);
        return ResultEntity.successWithoutData();
    }
}
